<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Aktifitas extends MX_Controller {
  function __construct() {
    $this->load->library('template');
    $this->load->library('form_eksternal');
    $this->load->library('global_models');
    $this->load->library('mobile_detect');
    $this->load->library('PHPExcel');
    $this->load->library('encrypt');
    $this->load->helper('path');
    $this->load->helper('url');
    $this->load->model('outlet/maktifitas');
    $this->load->library('parser');
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->load->library('pagination');
    $this->menu = $this->cek();

    if($this->session->userdata('outlet')){
       
      $this->cek_actifity($this->session->userdata('outlet'));
    }
    else{
     
      redirect("outlet/milik");
    }
   
  }
  function pica_edit($id){
//    $this->debug($this->input->post(NULL, TRUE));
    $field = explode("_", $this->input->post("id", TRUE));
    if($this->input->post("value", TRUE) == "Enter suggest")
      $post = "";
    else
      $post = $this->input->post("value", TRUE);
    $field_auther = "id_users_".$field[0];
    $this->global_models->update("d_pica", array("id_pica" => $id), 
            array($field[0] => $post, $field_auther => $this->session->userdata('id')));
    
    print $this->input->post("value", TRUE);
    
    die;
  }
  
   function pica_all_edit($id){
//    $this->debug($this->input->post(NULL, TRUE));
    $field = explode("_", $this->input->post("id", TRUE));
    if($this->input->post("value", TRUE) == "Enter suggest")
      $post = "";
    else
      $post = $this->input->post("value", TRUE);
    
    $this->global_models->update("d_history", array("id_history" => $id), 
            array($field[0] => $post));
    
    print $this->input->post("value", TRUE);
    
    die;
  }
  
  function history_edit($id){
//    $this->debug($this->input->post(NULL, TRUE));
    $field = explode("_", $this->input->post("id", TRUE));
    if($this->input->post("value", TRUE) == "Enter suggest")
      $post = "";
    else
      $post = $this->input->post("value", TRUE);
    
    $this->global_models->update("d_history", array("id_history" => $id), 
            array($field[0] => $post));
    
    print $this->input->post("value", TRUE);
    
    die;
  }
  function olah_editable_dinamis($table, $id, $id_param, $day = 0){
    $angka = $this->input->post('value', true);
    $angka = str_replace(".", "", $angka);
    $angka = str_replace(",", ".", $angka);
    
    $simpan = array(
        "id_actifity_report" => $id,
        "tanggal" =>  $day,
        "id_{$table}" =>  $id_param,
    );
    $cek_id = $this->global_models->get_field("d_day_actifity_{$table}", "id_day_actifity_{$table}", $simpan);
    
    if($cek_id > 0){
      $this->global_models->update("d_day_actifity_{$table}", $simpan, array(
          "qty" => $angka,
          "update_by_users" =>  $this->session->userdata('id')
          ));
    }
    else{
      $simpan["qty"] = $angka;
      $simpan["create_by_users"] = $this->session->userdata('id');
      $simpan["create_date"] = date("Y-m-d H:i:s");
      $simpan["update_by_users"] = $this->session->userdata('id');
      $this->global_models->insert("d_day_actifity_{$table}", $simpan);
    }
    
    print number_format($angka, 0, ",", ".");
    die;
  }
  function olah_editable($id, $day = 0){
    $angka = $this->input->post('value', true);
    $angka = str_replace(".", "", $angka);
    $angka = str_replace(",", ".", $angka);
    
    if(substr($this->input->post('id', true), 0, 7) == 'target_'){
      $field = str_replace('target_', "", $this->input->post('id', true));
      $simpan = array(
          "id_actifity_report" => $id,
          "tanggal" =>  0
      );
      $cek_id = $this->global_models->get_field("d_day_actifity_report", "id_day_actifity_report", $simpan);
      if($cek_id > 0){
        $this->global_models->update("d_day_actifity_report", $simpan, array(
            $field => $angka,
            "update_by_users" =>  $this->session->userdata('id')
            ));
      }
      else{
        $simpan[$field] = $angka;
        $simpan["create_by_users"] = $this->session->userdata('id');
        $simpan["create_date"] = date("Y-m-d H:i:s");
        $simpan["update_by_users"] = $this->session->userdata('id');
        $this->global_models->insert("d_day_actifity_report", $simpan);
      }
    }
    else if(substr($this->input->post('id', true), -2) == '_'.$day OR substr($this->input->post('id', true), -3) == '_'.$day){
      $field = str_replace('_'.$day, "", $this->input->post('id', true));
      $simpan = array(
          "id_actifity_report" => $id,
          "tanggal" =>  $day
      );
      $cek_id = $this->global_models->get_field("d_day_actifity_report", "id_day_actifity_report", $simpan);
      if($cek_id > 0){
        $this->global_models->update("d_day_actifity_report", $simpan, array(
            $field => $angka,
            "update_by_users" =>  $this->session->userdata('id')
            ));
      }
      else{
        $simpan[$field] = $angka;
        $simpan["create_by_users"] = $this->session->userdata('id');
        $simpan["create_date"] = date("Y-m-d H:i:s");
        $simpan["update_by_users"] = $this->session->userdata('id');
        $this->global_models->insert("d_day_actifity_report", $simpan);
      }
    }
    
    if($angka > 0)
      print number_format($angka, 0, ",", ".");
    else
      print 0;
    die;
  }
  
  function detail($id, $day = 0){
    $detail = $this->global_models->get("m_actifity_report", array("id_actifity_report"  =>  $id));
//    $pendapatan_lain = $this->maktifitas->get_pendapatan_lain("d_pendapatan_lain_outlet", "pendapatan_lain", $detail[0]->id_outlet);
    $opex = $this->maktifitas->get_pendapatan_lain("d_opex_outlet", "opex", $detail[0]->id_outlet);
    
    $data_detail = $this->global_models->get("d_day_actifity_report", array("id_actifity_report"  =>  $id));
    
    $hari_ke[] = $this->global_models->get_field("d_day_actifity_report", "MAX(tanggal)", array("id_actifity_report"  =>  $id));
    $hari_ke[] = $this->global_models->get_field("d_day_actifity_pendapatan_lain", "MAX(tanggal)", array("id_actifity_report"  =>  $id));
    $hari_ke[] = $this->global_models->get_field("d_day_actifity_opex", "MAX(tanggal)", array("id_actifity_report"  =>  $id));
    $jadi_hari = $hari_ke[0];
    if($jadi_hari < $hari_ke[1])
      $jadi_hari = $hari_ke[1];
    if($jadi_hari < $hari_ke[2])
      $jadi_hari = $hari_ke[2];
    if($jadi_hari <= 0)
      $jadi_hari = 1;
    /**
     * variable
     */
    
    if(is_array($opex)){
      foreach($opex as $opx){
        $data_opex_target[$opx->id_opex] = $this->global_models->get_field("d_day_actifity_opex", "qty", 
          array(
              "id_actifity_report"  =>  $id,
              "id_opex" => $opx->id_opex,
              "tanggal" =>  0
          ));
        for($i = 1 ; $i <= $detail[0]->day_in_month ; $i++){
          $data_opex_perday[$opx->id_opex][$i] = $this->global_models->get_field("d_day_actifity_opex", "qty", 
            array(
                "id_actifity_report"  =>  $id,
                "id_opex" => $opx->id_opex,
                "tanggal" =>  $i
            ));
        }
      }
    }
//    $this->debug($data_opex_target, true);
//    if(is_array($pendapatan_lain)){
//      foreach($pendapatan_lain as $pen){
//        $data_pendapatan_lain_target[$pen->id_pendapatan_lain] = $this->global_models->get_field("d_day_actifity_pendapatan_lain", "qty", 
//          array(
//              "id_actifity_report"  =>  $id,
//              "id_pendapatan_lain" => $pen->id_pendapatan_lain,
//              "tanggal" =>  0
//          ));
//        for($i = 1 ; $i <= $detail[0]->day_in_month ; $i++){
//          $data_pendapatan_lain_perday[$pen->id_pendapatan_lain][$i] = $this->global_models->get_field("d_day_actifity_pendapatan_lain", "qty", 
//            array(
//                "id_actifity_report"  =>  $id,
//                "id_pendapatan_lain" => $pen->id_pendapatan_lain,
//                "tanggal" =>  $i
//            ));
//        }
//      }
//    }
    
    $data_target = (object) array(
        "service" =>  0,
        "service_belanja" =>  0,
        "belanja" =>  0,
        "rata_service"  =>  0,
        "rata_belanja"  =>  0,
        "hpp"  =>  0,
        );
    for($i = 1 ; $i <= $detail[0]->day_in_month ; $i++){
      $data_harian[$i] = (object) array(
        "service" =>  0,
        "service_belanja" =>  0,
        "belanja" =>  0,
        "rata_service"  =>  0,
        "rata_belanja"  =>  0,
        "p_service"  =>  0,
        "p_service_plus"  =>  0,
        "p_belanja_plus"  =>  0,
        "p_belanja"  =>  0,
        "total_hpp"  =>  0,
      );
    }
    /**
     * variable
     */
    if(is_array($data_detail)){
      foreach($data_detail as $dd){
        if($dd->tanggal == 0){
          $data_target = $dd;
        }
        else{
          $data_harian[$dd->tanggal] = $dd;
        }
      }
    }
    
//    $this->debug($pendapatan_lain, true);
    $bulan = array(
        "1" =>  "Januari",
        "2" =>  "Februari",
        "3" =>  "Maret",
        "4" =>  "April",
        "5" =>  "Mei",
        "6" =>  "Juni",
        "7" =>  "Juli",
        "8" =>  "Agustus",
        "9" =>  "September",
        "10" =>  "Oktober",
        "11" =>  "Nopember",
        "12" =>  "Desember",
    );
    $this->template->title('Sistem', "Outlet");
//    $this->debug($this->maktifitas->get_privilege(), true);
    $view = "aktifitas-detail";
    if($this->mobile_detect->isMobile() OR $day > 0){
      $view = "mobile/m-aktifitas-detail";
    }
    $this->template->build($view, 
      array(
            'message'     => "",
            'url'     => base_url()."themes/srabon/",
            'detail'    => $detail,
            'pendapatan_lain'    => $pendapatan_lain,
            'opex'    => $opex,
            'menu_drop'   => $this->menu,
            'menu'        => "outlet",
            'title_table'    => "Laporan Harian ".$bulan[$detail[0]->month]." ".$detail[0]->year." Outlet ".$this->global_models->get_field("m_outlet", "name", array("id_outlet" => $detail[0]->id_outlet)),
            'foot'    => "",
            'data_target'    => array($data_target),
            'hari_ke'    => $jadi_hari,
            'data_harian'    => $data_harian,
            'data_opex_target'    => $data_opex_target,
            'data_pendapatan_lain_target'    => $data_pendapatan_lain_target,
            'data_opex_perday'    => $data_opex_perday,
            'data_pendapatan_lain_perday'    => $data_pendapatan_lain_perday,
            'privilege'    => "aktifitas_editable",
            'day_mobile'    => $day,
          ));
    $this->template
      ->set_layout('default')
      ->build($view);
  }
  
  function detail2($id, $day = 0){
    $detail = $this->global_models->get("m_actifity_report", array("id_actifity_report"  =>  $id));
//    $pendapatan_lain = $this->maktifitas->get_pendapatan_lain("d_pendapatan_lain_outlet", "pendapatan_lain", $detail[0]->id_outlet);
    $opex = $this->maktifitas->get_pendapatan_lain("d_opex_outlet", "opex", $detail[0]->id_outlet);
    
    $data_detail = $this->global_models->get("d_day_actifity_report", array("id_actifity_report"  =>  $id));
    
    $hari_ke[] = $this->global_models->get_field("d_day_actifity_report", "MAX(tanggal)", array("id_actifity_report"  =>  $id));
    $hari_ke[] = $this->global_models->get_field("d_day_actifity_pendapatan_lain", "MAX(tanggal)", array("id_actifity_report"  =>  $id));
    $hari_ke[] = $this->global_models->get_field("d_day_actifity_opex", "MAX(tanggal)", array("id_actifity_report"  =>  $id));
    $jadi_hari = $hari_ke[0];
    if($jadi_hari < $hari_ke[1])
      $jadi_hari = $hari_ke[1];
    if($jadi_hari < $hari_ke[2])
      $jadi_hari = $hari_ke[2];
    if($jadi_hari <= 0)
      $jadi_hari = 1;
    /**
     * variable
     */
    
    if(is_array($opex)){
      foreach($opex as $opx){
        $data_opex_target[$opx->id_opex] = $this->global_models->get_field("d_day_actifity_opex", "qty", 
          array(
              "id_actifity_report"  =>  $id,
              "id_opex" => $opx->id_opex,
              "tanggal" =>  0
          ));
        for($i = 1 ; $i <= $detail[0]->day_in_month ; $i++){
          $data_opex_perday[$opx->id_opex][$i] = $this->global_models->get_field("d_day_actifity_opex", "qty", 
            array(
                "id_actifity_report"  =>  $id,
                "id_opex" => $opx->id_opex,
                "tanggal" =>  $i
            ));
        }
      }
    }
//    $this->debug($data_opex_target, true);
//    if(is_array($pendapatan_lain)){
//      foreach($pendapatan_lain as $pen){
//        $data_pendapatan_lain_target[$pen->id_pendapatan_lain] = $this->global_models->get_field("d_day_actifity_pendapatan_lain", "qty", 
//          array(
//              "id_actifity_report"  =>  $id,
//              "id_pendapatan_lain" => $pen->id_pendapatan_lain,
//              "tanggal" =>  0
//          ));
//        for($i = 1 ; $i <= $detail[0]->day_in_month ; $i++){
//          $data_pendapatan_lain_perday[$pen->id_pendapatan_lain][$i] = $this->global_models->get_field("d_day_actifity_pendapatan_lain", "qty", 
//            array(
//                "id_actifity_report"  =>  $id,
//                "id_pendapatan_lain" => $pen->id_pendapatan_lain,
//                "tanggal" =>  $i
//            ));
//        }
//      }
//    }
    
    $data_target = (object) array(
        "service" =>  0,
        "service_belanja" =>  0,
        "belanja" =>  0,
        "rata_service"  =>  0,
        "rata_belanja"  =>  0,
        "hpp"  =>  0,
        );
    for($i = 1 ; $i <= $detail[0]->day_in_month ; $i++){
      $data_harian[$i] = (object) array(
        "service" =>  0,
        "service_belanja" =>  0,
        "belanja" =>  0,
        "rata_service"  =>  0,
        "rata_belanja"  =>  0,
        "p_service"  =>  0,
        "p_service_plus"  =>  0,
        "p_belanja_plus"  =>  0,
        "p_belanja"  =>  0,
        "total_hpp"  =>  0,
      );
    }
    /**
     * variable
     */
    if(is_array($data_detail)){
      foreach($data_detail as $dd){
        if($dd->tanggal == 0){
          $data_target = $dd;
        }
        else{
          $data_harian[$dd->tanggal] = $dd;
        }
      }
    }
    
//    $this->debug($pendapatan_lain, true);
    $bulan = array(
        "1" =>  "Januari",
        "2" =>  "Februari",
        "3" =>  "Maret",
        "4" =>  "April",
        "5" =>  "Mei",
        "6" =>  "Juni",
        "7" =>  "Juli",
        "8" =>  "Agustus",
        "9" =>  "September",
        "10" =>  "Oktober",
        "11" =>  "Nopember",
        "12" =>  "Desember",
    );
    $this->template->title('Sistem', "Outlet");
//    $this->debug($this->maktifitas->get_privilege(), true);
    $view = "aktifitas-detail2";
    if($this->mobile_detect->isMobile() OR $day > 0){
      $view = "mobile/m-aktifitas-detail";
    }
    $this->template->build($view, 
      array(
            'message'     => "",
            'url'     => base_url()."themes/srabon/",
            'detail'    => $detail,
            'pendapatan_lain'    => $pendapatan_lain,
            'opex'    => $opex,
            'menu_drop'   => $this->menu,
            'menu'        => "outlet",
            'title_table'    => "Laporan Harian ".$bulan[$detail[0]->month]." ".$detail[0]->year." Outlet ".$this->global_models->get_field("m_outlet", "name", array("id_outlet" => $detail[0]->id_outlet)),
            'foot'    => "",
            'data_target'    => array($data_target),
            'hari_ke'    => $jadi_hari,
            'data_harian'    => $data_harian,
            'data_opex_target'    => $data_opex_target,
            'data_pendapatan_lain_target'    => $data_pendapatan_lain_target,
            'data_opex_perday'    => $data_opex_perday,
            'data_pendapatan_lain_perday'    => $data_pendapatan_lain_perday,
            'privilege'    => "aktifitas_editable",
            'day_mobile'    => $day,
          ));
    $this->template
      ->set_layout('default')
      ->build($view);
  }
  
  public function add_pica(){
//    (, , "user_hidden", site_url("outlet/add_row"), "mul_users", "add_users", "nomor", "user", , "", "")
    $no = rand(100, 100000);
    $user = $this->session->userdata('name');
    $problem = $this->form_eksternal->form_textarea('problem[]', '', 'class="span9"');
    $corrective = $this->form_eksternal->form_textarea('corrective[]', '', 'class="span9"');
    $options = $this->global_models->get_dropdown("d_pic_users", "id_pic_users", 
            "id_users", TRUE, array("id_outlet" => $this->session->userdata('outlet')));
    if(is_array($options)){
      foreach ($options as $key => $op) {
        if($key > 0){
          $options[$key] = $this->global_models->get_field("m_users", "name", array("id_users" => $op));
        }
      }
    }
    $pic = $this->form_eksternal->form_autocomplate("pic_view[]", "pic_desk[]", 
                              "id_pic_view_{$no}", "id_pic_desk_".$no, site_url("outlet/aktifitas/pic-users"), 
                              "", 0, "class='span8'",
                              ""
                              );
    //$tanggal = $this->form_eksternal->form_input('tanggal[]', date("Y-m-d H:i:s"), 'readonly class="span11"');
    $tanggal = date("d M Y");
    $datenya = $this->form_eksternal->form_date('due_date[]', '', 'due_date_pic_'.$no, 'class="span10"');
    print <<<EOD
    <tr id="tr_{$no}">
      <td>
        <div class="username">{$user}</div>
        <div class="create_date">{$tanggal}</div>
      </td>
      <td colspan="2">{$problem}</td>
    </tr>
EOD;
    die;
  }
  public function add_history($id){
     $id_usr = $this->global_models->get_field("d_pic_users", "id_users", array("id_outlet" => $this->session->userdata('outlet')));
    $view_pic = $this->global_models->get_field("m_users", "name", array("id_users" => $id_usr));
    $id_privi = $this->global_models->get_field("d_user_privilege", "id_privilege", array("id_users" => $id_usr));
    $view_pic .= " | ". $this->global_models->get_field("m_privilege", "name", array("id_privilege" => $id_privi));
     $id_pic_users = $this->global_models->get_field("d_pic_users", "id_pic_users", array("id_users" => $id_usr));
    $nm_bc = $this->global_models->get_field("m_users", "name", array("id_users" => $id_usr,"id_status_user" => "1"));
//    (, , "user_hidden", site_url("outlet/add_row"), "mul_users", "add_users", "nomor", "user", , "", "")
    $no = rand(100, 100000);
    $problem = $this->form_eksternal->form_textarea('problem_'.$id.'[]', '', 'class="span12"');
    $corrective = $this->form_eksternal->form_textarea('corrective_'.$id.'[]', '', 'class="span12"');
    $options = $this->global_models->get_dropdown("d_pic_users", "id_pic_users", 
            "id_users", TRUE, array("id_outlet" => $this->session->userdata('outlet')));
    if(is_array($options)){
      foreach ($options as $key => $op) {
        if($key > 0){
          $options[$key] = $this->global_models->get_field("m_users", "name", array("id_users" => $op));
        }
      }
    }
    $pic = $this->form_eksternal->form_autocomplate("pic_view_{$id}[]", "pic_desk_{$id}[]", 
                              "id_pic_view_{$no}", "id_pic_desk_".$no, site_url("outlet/aktifitas/pic-users"), 
                              $view_pic, $id_pic_users, "class='span8'",
                              ""
                              );
    $tanggal = $this->form_eksternal->form_input('tanggal_'.$id.'[]', date("Y-m-d H:i:s"), 'readonly class="span11"');
    $datenya = $this->form_eksternal->form_date('due_date_'.$id.'[]', '', 'due_date_pic_'.$no, 'class="span10"');
    $cek_list = $this->form_eksternal->form_checkbox('cek[]', 0, FALSE, " id='cekprivilege' disabled");
                          
    print <<<EOD
    <tr id="tr_{$no}">
      <td>{$tanggal}</td>
      <td>{$problem}</td>
      <td>{$corrective}</td>
      <td>{$pic}</td>
      <td>{$datenya}</td>
        <td>{$cek_list}</td>
    </tr>
EOD;
    die;
  }
  function pica_exels($id){
      
    $this->maktifitas->pica_xls($id);
  }
  
  function fs_export_xls($id){
    $this->maktifitas->fs_export_xls($id);
    die;
  }
  function half_export_xls($id){
    $this->maktifitas->half_export_xls($id);
  }
  function history_exels($id){
    $this->maktifitas->history_xls($id);
  }
  function pica($id){
    $detail = $this->global_models->get("m_actifity_report", array("id_actifity_report"  =>  $id));
    $data = $this->global_models->get("d_pica", array("id_actifity_report"  =>  $id));
    $this->template->title('Sistem', "Outlet");
    $bulan = array(
        "1" =>  "Januari",
        "2" =>  "Februari",
        "3" =>  "Maret",
        "4" =>  "April",
        "5" =>  "Mei",
        "6" =>  "Juni",
        "7" =>  "Juli",
        "8" =>  "Agustus",
        "9" =>  "September",
        "10" =>  "Oktober",
        "11" =>  "Nopember",
        "12" =>  "Desember",
    );
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('pica-detail', 
        array(
              'message'     => "",
              'url'     => base_url()."themes/srabon/",
              'detail'    => $detail,
              'data'    => $data,
              'menu_drop'   => $this->menu,
              'menu'        => "outlet",
              'title_table'    => "History ".$bulan[$detail[0]->month]." ".$detail[0]->year." Outlet ".$this->global_models->get_field("m_outlet", "name", array("id_outlet" => $detail[0]->id_outlet)),
              'foot'    => ""
            ));
      $this->template
        ->set_layout('default')
        ->build('pica-detail');
    }
    else{
//      $this->debug($this->input->post(NULL, TRUE), true);      
      $pst = $this->input->post(NULL, TRUE);      
      $no = 0;
      foreach($pst['problem'] as $key => $ptanggal){
        $kirim[$no] = array(
            "id_actifity_report"  =>  $this->input->post("id_detail",true),
            "create_by_users"     =>  $this->session->userdata('id'),
            "create_date"         =>  date('Y-m-d H:i:s'),
            "update_by_users"     =>  $this->session->userdata('id'),
        );
        if($pst['problem'][$key]){
          $kirim[$no]['problem'] = $pst['problem'][$key];
          $kirim[$no]['id_users_problem'] = $this->session->userdata('id');
        }
        $no++;
      }    
//      $this->debug($kirim,true);
      if($this->global_models->insert_batch("d_pica", $kirim))
        redirect ("outlet/aktifitas/pica/{$this->input->post("id_detail",true)}/sukses");
      else
        redirect ("outlet/aktifitas/pica/{$this->input->post("id_detail",true)}/filed");
    }
  }
  
  function history($id){
    $detail = $this->global_models->get("m_actifity_report", array("id_actifity_report"  =>  $id));
    $this->template->title('Sistem', "Outlet");
    $bulan = array(
        "1" =>  "Januari",
        "2" =>  "Februari",
        "3" =>  "Maret",
        "4" =>  "April",
        "5" =>  "Mei",
        "6" =>  "Juni",
        "7" =>  "Juli",
        "8" =>  "Agustus",
        "9" =>  "September",
        "10" =>  "Oktober",
        "11" =>  "Nopember",
        "12" =>  "Desember",
    );
    $parent = array(
        1 => "SDM",
        2 => "Jasa service  dan Part",
        3 => "Administrasi",
        4 => "Marketing dan Promo",
        5 => "Tools",
    );
    foreach ($parent as $ky => $prt){
      $data[$ky] = $this->global_models->get("d_history", array("id_actifity_report"  =>  $id, "kategori" => $ky));
    }
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('history-detail', 
        array(
              'message'     => "",
              'url'     => base_url()."themes/srabon/",
              'menu_drop'   => $this->menu,
              'menu'        => "outlet",
              'detail'        => $detail,
              'data'        => $data,
              'parent'        => $parent,
              'title_table'    => "PICA ".$bulan[$detail[0]->month]." ".$detail[0]->year." Outlet ".$this->global_models->get_field("m_outlet", "name", array("id_outlet" => $detail[0]->id_outlet)),
              'foot'    => ""
            ));
      $this->template
        ->set_layout('default')
        ->build('history-detail');
    }
    else{
      $pst = $this->input->post(NULL, TRUE);
      $no = 0;
      $kirim = array();
      foreach ($parent as $ky => $prt){
        if(is_array($pst['tanggal_'.$ky])){
          foreach($pst['tanggal_'.$ky] as $key => $ptanggal){
            if($pst['problem_'.$ky][$key]){
              $kirim[$no] = array(
                  "id_actifity_report"  =>  $this->input->post("id_detail",true),
                  "kategori"            =>  $ky,
                  "create_by_users"     =>  $this->session->userdata('id'),
                  "create_date"         =>  $ptanggal,
                  "update_by_users"     =>  $this->session->userdata('id'),
              );
              if($pst['problem_'.$ky][$key]){
                $kirim[$no]['problem'] = $pst['problem_'.$ky][$key];
              }
              if($pst['corrective_'.$ky][$key]){
                $kirim[$no]['corrective'] = $pst['corrective_'.$ky][$key];
              }
              if($pst['pic_desk_'.$ky][$key] > 0){
                $kirim[$no]['id_pic_users'] = $pst['pic_desk_'.$ky][$key];
              }
              if($pst['due_date_'.$ky][$key]){
                $kirim[$no]['due_date'] = $pst['due_date_'.$ky][$key];
              }
            }
            $no++;
          }
        }
      }
      if($kirim){
        if($this->global_models->insert_batch("d_history", $kirim))
          redirect ("outlet/aktifitas/history/{$this->input->post("id_detail",true)}/sukses");
        else
          redirect ("outlet/aktifitas/history/{$this->input->post("id_detail",true)}/filed");
      }
      else
        redirect ("outlet/aktifitas/history/{$this->input->post("id_detail",true)}");
    }
  }
  private function cek_actifity($id){
  $bulan = (int) date("m");
    if($this->global_models->get_field("m_actifity_report", "id_actifity_report", 
            array("month"  =>  $bulan, "year" => date("Y"), "id_outlet" => $id)) === FALSE){
      $this->global_models->insert("m_actifity_report", 
              array(
                  "id_outlet" =>  $id,
                  "name"      =>  "Laporan ".date("F")." ".date("Y"),
                  "month"     =>  date("m"),
                  "day_in_month"     =>  date("t"),
                  "year"      =>  date("Y"),
                  "create_by_users" =>  1,
                  "create_date" =>  date("Y-m-d H:i:s"),
                  "update_by_users" =>  1
                  ));
    }
    return true;
  }
	function pic_users(){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
// remove slashes if they were magically added
    if (get_magic_quotes_gpc()) $q = stripslashes($q);

    $items = $this->maktifitas->get_user_auto($q);
    $result = array();
    foreach ($items as $key=>$value) {
      if (strpos(strtolower($key), $q) !== false) {
        array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
      }
      if (count($result) > 11)
        break;
    }

// json_encode is available in PHP 5.2 and above, or you can install a PECL module in earlier versions
    echo json_encode($result);
    die;
  }
	function post_pic(){
    $this->global_models->update("d_pica", array("id_pica" => $this->input->post("id", TRUE)), 
            array("id_pic_users" => $this->input->post("id_pic_users", TRUE)));
    
    print $this->input->post("value", TRUE);
    
    die;
  }
	function post_pic_history(){
    $this->global_models->update("d_history", array("id_history" => $this->input->post("id", TRUE)), 
            array("id_pic_users" => $this->input->post("id_pic_users", TRUE)));
    
    print $this->input->post("value", TRUE);
    
    die;
  }
	function post_due_date(){
    $this->global_models->update("d_pica", array("id_pica" => $this->input->post("id", TRUE)), 
            array("due_date" => $this->input->post("due_date", TRUE), "id_users_pic" => $this->session->userdata("id")));
    
    print $this->input->post("value", TRUE);
    
    die;
  }
	function post_due_date_history(){
    $this->global_models->update("d_history", array("id_history" => $this->input->post("id", TRUE)), 
            array("due_date" => $this->input->post("due_date", TRUE)));
    
    print $this->input->post("value", TRUE);
    
    die;
  }
  
  
  public function add_new_report($id = 0, $pesan = "hal") {
      $data_detail['bulan'] = array(
        '1' => 'Januari',
        '2' => 'Februari',
        '3' => 'Maret',
        '4' => 'April',
        '5' => 'Mei',
        '6' => 'Juni',
        '7' => 'Juli',
        '8' => 'Agustus',
        '9' => 'September',
        '10' => 'Oktober',
        '11' => 'November',
        '12' => 'Desember'
      );      
      
      $year_now = date('Y', strtotime('NOW'));
      $year_min_range = $year_now-1;
      $year_max_range = $year_now+1;
            
      $data_detail['tahun'] = array($year_min_range => $year_min_range, $year_now => $year_now, $year_max_range => $year_max_range);
            
      if(!$this->input->post(NULL, TRUE)){
      $this->template->build('add-new-report', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Report',
              'data'    => $data_detail,
              'menu_drop'   => $this->menu,
              'menu'        => "settings",
              'foot'    => '
                <script>
                    $(function () {                    
                    });
                </script>
                '
            ));
      $this->template
        ->set_layout('default')
        ->build('add-new-report');
    }
    else{
        $pst = $this->input->post(NULL, TRUE);
        if($pst['bulan']){  
          $laporan_detail = array(
              'month'   =>  $pst['bulan'],
              'year'    =>  $pst['tahun'],
              'id_outlet' => $this->session->userdata('outlet')
          );
                    
          //cek apakah laporan sudah ada atau belum
          $laporan = $this->global_models->get('m_actifity_report', $laporan_detail);
          
          if(!$laporan) {
            $kirim = array(
              "id_outlet"              =>  $this->session->userdata('outlet'),
              "name"                   =>  'Laporan '.$data_detail['bulan'][$pst['bulan']].' '.$pst['tahun'],
              "month"                  =>  $pst['bulan'],
              "day_in_month"           =>  cal_days_in_month(CAL_GREGORIAN, $pst['bulan'], $pst['tahun']),
              "year"                  =>  $pst['tahun'],
              "create_by_users"       =>  $this->session->userdata('id'),
              "create_date"           =>  date("Y-m-d H:i:s"),
              "update_by_users"       =>  $this->session->userdata('id')
            );
            
            if($this->global_models->insert("m_actifity_report", $kirim))
                redirect ("outlet/aktifitas/daftar/sukses");
            else{
                redirect ("outlet/aktifitas/daftar/filed");
            }
          }
          else {
              redirect ("outlet/aktifitas/daftar/filed");
          }  
        }
        else{
          redirect ("outlet/index/list/filed");
        }
    }
  }
  
	function daftar(){
   
   
    if($this->input->post("cek", TRUE) != ""){
    //  foreach($this->input->post("cek", TRUE) as $ck){
      $this->maktifitas->fs_export_xls1($this->input->post("cek", TRUE),$this->global_models->get_field("m_outlet", "name", array("id_outlet" => $this->session->userdata('outlet'))));
      //  $this->fs_export_xls();
       
      //}
    }
   
   
    //$this->fs_export_xls($id);
    if($this->session->userdata("outlet") <= 0){
      redirect("outlet/milik");
    }
   
    $list = $this->global_models->get_query("
      SELECT A.*, B.name as outlet
      FROM m_actifity_report AS A
      LEFT JOIN m_outlet AS B ON A.id_outlet = B.id_outlet
      WHERE A.id_outlet = {$this->session->userdata('outlet')}
      ORDER BY A.year DESC, A.month DESC
      ");
    $this->template->title('Sistem', "Outlet");
    
    $view = "aktifitas-daftar";
    if($this->mobile_detect->isMobile()){
      $view = "mobile/m-aktifitas-daftar";
    }
//    $this->debug($list, true);
    $this->template->build($view, 
      array(
            'message'     => "",
            'url'     => base_url()."themes/srabon/",
            'data'    => $list,
            'menu_drop'   => $this->menu,
            'menu'        => "outlet",
            'title_table'    => "Data Laporan Bulanan Outlet ".$this->global_models->get_field("m_outlet", "name", array("id_outlet" => $this->session->userdata('outlet'))),
            'foot'    => ""
          ));
    $this->template
      ->set_layout('default')
      ->build($view);
  }
	function unit_entry_chart($id){
    $this->template->title('Sistem', "Outlet");
    $data_aktifitas = $this->global_models->get("m_actifity_report", array('id_actifity_report' => $id));
    $data_report = $this->global_models->get_dropdown("d_day_actifity_report", "tanggal", "service", FALSE, array('id_actifity_report' => $id));
//    $this->debug($data_aktifitas, true);
    $query = "
      SELECT 
      tanggal,
      (service + service_belanja) AS total_service,
      (service_belanja + belanja) AS total_belanja,
      (service + service_belanja + belanja) AS total_transaksi,
      (p_service + p_service_plus) AS rupiah_service,
      (p_belanja + p_belanja_plus) AS rupiah_belanja,
      (p_service + p_service_plus + p_belanja + p_belanja_plus) AS rupiah_transaksi
      FROM d_day_actifity_report
      WHERE
      id_actifity_report = {$id} AND tanggal > 0
      ORDER BY tanggal ASC
      ";
      
    $dataunit = $this->global_models->get_query($query);
    
    $query_target = "
      SELECT 
      tanggal,
      ((service + service_belanja)/{$data_aktifitas[0]->day_in_month}) AS total_service,
      ((service_belanja + belanja)/{$data_aktifitas[0]->day_in_month}) AS total_belanja,
      ((service + service_belanja + belanja)/{$data_aktifitas[0]->day_in_month}) AS total_transaksi,
      ((service * rata_service + service_belanja * rata_service)/{$data_aktifitas[0]->day_in_month}) AS rupiah_service,
      ((belanja * rata_belanja + service_belanja * rata_belanja)/{$data_aktifitas[0]->day_in_month}) AS rupiah_belanja,
      (((service * rata_service + service_belanja * rata_service) + (belanja * rata_belanja + service_belanja * rata_belanja))/{$data_aktifitas[0]->day_in_month}) AS rupiah_transaksi
      FROM d_day_actifity_report
      WHERE
      id_actifity_report = {$id} AND tanggal = 0
      ORDER BY tanggal ASC
      ";
      
    $dataunit_target = $this->global_models->get_query($query_target);
    $res = array();
    for($i = 1 ; $i <= $data_aktifitas[0]->day_in_month ; $i++){
      $target = array(
          "target_service"  =>  $dataunit_target[0]->total_service * $i,
          "target_belanja"  =>  $dataunit_target[0]->total_belanja * $i,
          "target_transaksi"  =>  $dataunit_target[0]->total_transaksi * $i,
          "target_rupiah_service"  =>  $dataunit_target[0]->rupiah_service * $i,
          "target_rupiah_belanja"  =>  $dataunit_target[0]->rupiah_belanja * $i,
          "target_rupiah_transaksi"  =>  $dataunit_target[0]->rupiah_transaksi * $i,
      );
      foreach ($dataunit as $key => $value) {
        if($value->tanggal == $i){
          $res[$data_aktifitas[0]->year."-".$data_aktifitas[0]->month."-".$i] = (object)array_merge((array)$value, $target);
//          unset($dataunit[$key]);
          break;
        }
        else{
          $res[$data_aktifitas[0]->year."-".$data_aktifitas[0]->month."-".$i] = (object)array_merge(array(
            "tanggal" => $i,
            "total_service" => 0,
            "total_belanja" => 0,
            'total_transaksi' => 0,
            "rupiah_service" => 0,
            "rupiah_belanja" => 0,
            "rupiah_transaksi" => 0,
          ), $target);
        }
      }
    }
    
//    $this->debug($res,true);
    
    $list = $this->global_models->get_query("
      SELECT A.*, B.name as outlet
      FROM m_actifity_report AS A
      LEFT JOIN m_outlet AS B ON A.id_outlet = B.id_outlet
      WHERE A.id_outlet = {$this->session->userdata('outlet')}
      ORDER BY A.year DESC, A.month DESC
      ");
      
    foreach($list AS $ls){
      $data_list[$ls->id_actifity_report] = $ls->name;
    }
    
    $costume_query = $this->global_models->get_query("SELECT service, service_belanja"
      . " FROM d_day_actifity_report"
      . " ORDER BY service DESC LIMIT 0, 10");
    $no = 1;
    foreach($costume_query AS $kk => $cq){
      $costume1 .= <<<EOD
        ['2014-12-{$no}', {$cq->service}],
EOD;
      $costume2 .= <<<EOD
        ['2014-12-{$no}', {$cq->service_belanja}],
EOD;
        $no++;
    }
    
//    $this->debug($costume1);
//    $this->debug($costume2, true);
    
    $name_outlet = $this->global_models->get_field("m_outlet", "name", array("id_outlet" => $this->session->userdata('outlet')));
    $this->template->build('unit-entry-chart', 
      array(
            'message'     => "",
            'url'     => base_url()."themes/srabon/",
            'menu_drop'   => $this->menu,
            'menu'        => "outlet",
			'name_outlet' => $name_outlet,
            'res'         =>  $res,
            'data_service'=> $data_service,
            'id_actifity_report_new'     => $id,
            'foot'    => "",
            'list'    => $data_list,
            'costume1'  => $costume1,
            'costume2'  => $costume2,
          ));
    $this->template
      ->set_layout('default')
      ->build('unit-entry-chart');
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */